<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\livebroadcast\controller;

use plugin\saiadmin\basic\BaseController;
use app\livebroadcast\logic\liveBroadcastLogic;
use app\livebroadcast\validate\liveBroadcastValidate;
use support\Request;
use support\Response;

/**
 * 厂播列表表控制器
 */
class liveBroadcastController extends BaseController
{
    /**
     * 数据表主键
     */
    protected $pk = 'broadcast_id';

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new liveBroadcastLogic();
        $this->validate = new liveBroadcastValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['broadcast_title', ''],
            ['broadcast_type', ''],
            ['cate', ''],
            ['enterprise_name', ''],
            ['lang_code', ''],
        ]);
        $query = $this->logic->search($where) ->order('sort_order desc,create_time desc');
        $data = $this->logic->getList($query);
        return $this->success($data);
    }

    public function changeStatus(Request $request): Response
    {
        $id = $request->input('id', '');
        $status = $request->input('status', 1);
        $type = $request->input('type', 1);
        if ($type == 1) {
            $datas['is_display'] = $status;
        }
        $result = $this->logic->where($this->pk, $id)->update($datas);

        if ($result) {
            // $this->afterChange('changeStatus');
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

    public function updateSortOrder(Request $request): Response
    {
        $id = $request->input('id', '');
        $sort_order = $request->input('sort_order', 1);

        $result = $this->logic->where($this->pk, $id)->update(['sort_order' => $sort_order, 'update_time' => date('Y-m-d H:i:s')]);

        if ($result) {
            $this->afterChange('changeStatus');
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }

    }

}
